<?php
namespace soa\partner\zt;
use artisan\cache;
use artisan\http;
use artisan\format;
use service\pda\ztPdaService;

class ztThirdPartyBranch
{
    //配置信息
    //接口地址(测试用)
    protected $zto_service_url = 'http://testpartner.zto.cn/client/interface.php';
    //服务项目名称(测试用)
    protected $zto_partner = 'test';
    //erp系统签名(测试用)
    protected $zto_pass = 'ZTO123';

    //接口地址(正式环境用)
    //protected $zto_service_url = 'http://partner.zto.cn/client/interface.php';
    //服务项目名称(正式环境用)
    //protected $zto_partner = '';
    //erp系统签名(正式环境用)
    //protected $zto_pass = '';

    protected $account;
    protected $openId;

    public function getBrance($account)
    {
//logDebug($account,'third');
        if(!$account || !is_array($account)){
            raise(4016, "快递员信息有误");
        }else{
            $empNo = $account['emp_no'];
            $openId = \artisan\cache::get($empNo ."openid:new");   //获取openid
//logDebug($openId,'third');
            if(empty($openId)){//若无openid重新登录
                $ztObj = new \soa\parter\zt\pda();
                $openId = $ztObj->login($account);
//logDebug($openId,'third');
                if(empty($openId)){
                    raise(4016, "系统异常，请稍后重试");
                }
            }
        }

//logDebug($account,'third');
//logDebug($openId,'third');
        $date = '2000-01-01 00:00:00';
        $p_v = $account['branch_no'].$date;
        $length = strlen($p_v);
        $xml = '<?xml version="1.0" encoding="utf-8" ?>
<root>
<h>
  <ver>4.0</ver>
  <optype>G026</optype>
  <manufacturer>KUAIBAOAPP</manufacturer>
  <u_company>' . $account['branch_no'] . '</u_company>
  <u_code>' . $empNo . '</u_code>
  <sys_sn>' . $account['imei'] . '</sys_sn>
  <key>' . $account['key'] . '</key>
  <openid>'.$openId.'</openid>
</h>
 <d>
  <h>' . $length. '</h>
  <p>
      <a>' . $account['branch_no'] . '</a>
      <b>' . $date . '</b>
  </p>
</d>
</root>';

        $result = $this->sendToZto($xml);
//logDebug($result,'third');
        $return = $tmp = [];
        if(isset($result['a']) && isset($result['b'])){
            $tmp['no'] = $result['a'];
            $tmp['name'] = $result['b'];
            $return[] = $tmp;
            $isOk = \artisan\cache::connect('pda')->set('ztThirdParty_'.$account['branch_no'],$return,3600);
        }
        if($result && !isset($result['a'])){
            foreach($result as $val){
                $tmp['no'] = $val['a'];
                $tmp['name'] = $val['b'];
                $return[] = $tmp;
            }
            $isOk = \artisan\cache::connect('pda')->set('ztThirdParty_'.$account['branch_no'],$return,3600);
        }
//logDebug($return,'third');
        if($return && is_array($return)){
            return $return;
        }else{
            return [];
        }
    }

    public function sendToZto($xml)
    {
        (mb_internal_encoding('UTF-8'));
        $post = array(
            'data_xml'  => mb_convert_encoding($xml, 'GBK'),
            'data_sign' => ( base64_encode(md5($xml.'123456', true)) ),
            'data_user' => 'KUAIBAOAPP',
            'data_ver' => '4.0'
        );

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:multipart/form-data; charset=utf-8',
                'content' => http_build_query($post)
            )
        );
        $retXml = http::post('http://pda.zto.cn/pda.zto', $post, array('HTTPHEADER' => array(
            'Content-type:multipart/form-data; charset=utf-8'
        )));
        $ret = format::xml2array($retXml);

//logDebug('result1','third');
//logDebug($retXml,'third');
//logDebug($ret,'third');
//logdebug($ret['Success']['d']['p'],'third');
//logDebug('result2','third');

        if(isset($ret['Success'])){
            $return = empty($ret['Success']['d']['p']) ? '' : $ret['Success']['d']['p'];
        }else{
            $return = array();
        }
        return $return;
    }
}